package de.gameduell.framework.game.billard.controller {
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.game.billard.model.Ball;
	import de.gameduell.framework.util.Tools;
	
	import org.cove.ape.Vector;	

	/**
	 * @author cbra
	 */
	public class BillardParser {
		/**
		 * parses the ball ids and put them into an array
		 * 
		 * @return list of ball ids
		 */
		public static function parseBallIds(aBallIds:String):Array{
			Debug.trace("parseBallIds");
			var idStrings:Array = aBallIds.split(",");
			var ids:Array = new Array();
			for(var i:* in idStrings){
				ids.push(int(idStrings[i]));
			}
			return aBallIds == "" ? new Array() : ids;
		}
		
		/**
		 * parses the ball positions and put them into an array
		 * 
		 * @return ball position list of all balls
		 */
		public static function parsePositions(posString:String):Array{
			var positionStrings:Array = posString.split(";");
			var positions:Array = new Array();
			for (var i in positionStrings){			
				var positionString:String = positionStrings[i];
				var posVector:Vector = parsePosition(positionString);
				positions.push(posVector);
			}
			return positions;
		}
		
		/**
		 * parses the position of one ball and return it as a vector
		 * 
		 * @return position vector
		 */
		public static function parsePosition(aString:String):Vector{
			var position:Array = aString.split(",");
			return new Vector(Tools.toFixedNumber(Number(position[0])),Tools.toFixedNumber(Number(position[1])));	
		}
		
		public static function encodePocketedBalls(pocketedBalls:Array):String {
			if(pocketedBalls.length == 0) {
				return "";
			}else {
				return pocketedBalls.join(";");
			}
		}

		public static function encodePositions(ballList:Array):String {
			var positions:Array = new Array();
			for(var i:* in ballList) {
				var ball:Ball = ballList[i];
				positions.push(encodePosition(ball.position));
			}
			return positions.join(";");			
		}

		/**
		 * encode a position vector into a string
		 * 
		 * @return position as a string
		 */
		public static function encodePosition(aVector:Vector):String{
			return ""+Tools.toFixedNumber(aVector.x)+","+Tools.toFixedNumber(aVector.y);
		}
	}
}
